package cn.fuck;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class ChangeStreamByMap {

    public static Stream<String> letters(String s) {
        List<String> word = new ArrayList<>();
        for (int i = 0; i < s.length(); i++) {
            word.add(s.substring(i, i + 1));
        }
        return word.stream();
    }

    public static void main(String[] args) throws IOException {
        String wordString = new String(Files.readAllBytes(Paths.get("" +
                "D:\\IDEA\\Code22\\src\\cn\\fuck\\word.txt")));
        List<String> words = Arrays.asList(wordString.split("\\W+"));
        //System.out.println(words);

        // map的函数作用到每一个元素上
        Stream<String> lower = words.stream().map(String::toLowerCase);
        Tool.show("lower", lower);
        // 得到首字母
        Stream<String> first = words.stream().map(s -> s.substring(0, 1));
        Tool.show("first letter of Words", first);
        // letters方法本身会产生流, flatMap可以把嵌套的流摊平
        Stream<String> flat = words.stream().flatMap(ChangeStreamByMap::letters);
        Tool.show("flat", flat);
    }
}
